package com.survey.Controller;


import com.survey.pojo.data.SurveyDO;
import com.survey.pojo.vo.AnswersheetVO;
import com.survey.pojo.vo.SurveyVO;
import com.survey.service.AnswersheetService;
import com.survey.service.SurveyService;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
public class SurveyAddController {
    @Autowired
    SurveyService surveyService;
    @Autowired
    AnswersheetService answersheetService;

    @RequestMapping("/surveyManage/addSurvey.do")
    @ResponseBody
    public String addSurvey(SurveyVO surveyVO, HttpServletRequest request) throws Exception {

        SurveyDO surveyDO=surveyService.find(surveyVO.getName());
        if (surveyDO!=null){
            return "问卷名已存在";
        }
        String endTime = request.getParameter("endTime");
        String isOpen = request.getParameter("isOpen");
        //String path = request.getParameter("myiframe");
        String img = (String) request.getSession().getAttribute("img");
        String survey_password2 = request.getParameter("Survey_Password2");
        // String img = request.getParameter("img");
        surveyVO.setImg(img);
        surveyVO.setPassword(survey_password2);
        SimpleDateFormat aDate=new SimpleDateFormat("yyyy-mm-dd");
        //Date date=aDate.parse(endTime);
        surveyVO.setEndtime(aDate.parse(endTime));
        surveyVO.setCreatedate(new Date());
        if (isOpen!=null){
            surveyVO.setIsopen(1);
        }else {
            surveyVO.setIsopen(0);
        }
        request.getSession().removeAttribute("img");
        String addSurvey = surveyService.addSurvey(surveyVO);
        return addSurvey;



    }

    @RequestMapping("/surveyManage/list")
    public String list(Model model,SurveyVO query) throws Exception {
        //SurveyVO query=new SurveyVO();
        //List<SurveyVO> list=surveyService.listAllSurvey();
        int PageSize=5;
        int Size=5;
        query.setPageSize(PageSize);
        query.setSize(Size);
        query.setIsopen(1);
        PageBean<SurveyVO> pageBean=surveyService.listSurvey(query);
        model.addAttribute("pageBean", pageBean);
        return "/admin/SurveyStatis.jsp";

    }

    @RequestMapping("/surveyManage/ShowSheets/{sid}")
    public String ShowSheets(@PathVariable Integer sid,AnswersheetVO answersheetVO, Model model) throws Exception {
        answersheetVO.setPageSize(5);
        answersheetVO.setSize(5);
        answersheetVO.setFkSurvey(sid);

       PageBean<AnswersheetVO> pageBean = answersheetService.listAnswersheet(answersheetVO);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("sid",sid);
        return "/admin/ShowSheets.jsp";


    }



    @RequestMapping("/surveyManage/upload")
    @ResponseBody
    public String upload(HttpServletRequest request, MultipartFile picture) throws Exception {


        String ext = FilenameUtils.getExtension(picture.getOriginalFilename());
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        //String path=request.getServletContext().getRealPath("/UploadImg");
        picture.transferTo(new File(request.getServletContext().getRealPath("/UploadImg")+"/"+name+"."+ext));
        request.getSession().setAttribute("img",name+"."+ext);
        return name+"."+ext;

    }

    @RequestMapping("/surveyManage/Delete")
    @ResponseBody
    public Result find(Integer asid,Integer sid) throws Exception {
        AnswersheetVO answersheetVO=new AnswersheetVO();
        answersheetVO.setFkSurvey(sid);
        answersheetVO.setId(asid);
        return answersheetService.deleteAnswersheet(answersheetVO);



    }

   @RequestMapping("/surveyManage/SurveyStatisShow")
    public String SurveyStatisShow(Integer sid){



        return "/admin/SurveyStatisShow.jsp";
    }


}
